// pages/hobby/hobby.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hobbyData: [{ txt: '阅读', checked: false }, { txt: '运动', checked: false }, { txt: '旅游', checked: false },
    { txt: '音乐', checked: false }, { txt: '影视', checked: false }, { txt: '书法', checked: false },
    { txt: '作画', checked: false }
    ],
    // canSubmit:false,
    // hobby:'',
    formData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'personalInfo',
      success: function (res) {
        that.data.hobbyData.forEach(function (ele) {
          if (res.data.hobby.indexOf(ele.txt) >= 0) {
            ele.checked = true;
          }
        })
        that.setData({
          // hobby: res.data.hobby,
          hobbyData: that.data.hobbyData,
          formData: res.data
        })
      }
    })
  },
  selOther(e) {//选择其他需求事件
    var label_arry = [];
    var index = e.target.dataset.index;
    if (this.data.hobbyData[index].checked == false) {
      this.data.hobbyData.forEach(function (ele) {
        if (ele.checked == true) {
          label_arry.push(ele.txt);
        }
      })
      if (label_arry.length >= 3) {
        wx.showModal({
          title: '提示',
          content: '最多只能选择三个',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      } else {
        this.data.hobbyData[index].checked = !this.data.hobbyData[index].checked;
        this.setData({
          hobbyData: this.data.hobbyData
        });
      }

    } else {
      this.data.hobbyData[index].checked = !this.data.hobbyData[index].checked;
      this.setData({
        hobbyData: this.data.hobbyData
      });
    }
    // var arr=[];
    // this.data.hobbyData.forEach(function (ele) {
    //   if (ele.checked == true) {
    //     arr.push(ele.txt);
    //   }
    // })
    // if (arr.length>0){
    //   this.setData({
    //     canSubmit: true
    //   });
    // }else{
    //   this.setData({
    //     canSubmit: false
    //   });
    // }
  },
  saveHobby: function (e) {
    var arr = [];
    this.data.hobbyData.forEach(function (ele) {
      if (ele.checked == true) {
        arr.push(ele.txt);
      }
    })
    this.data.formData.hobby = arr.join(',');
    wx.setStorage({
      key: 'personalInfo',
      data: this.data.formData,
    })
    wx.navigateTo({
      url: "/pages/perfectInfo/perfectInfo?flag=true"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})